package com.zhudi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhudi.annotation.UserLoginCheck;
import com.zhudi.commen.BaseResponse;
import com.zhudi.model.enums.ErrorCode;
import com.zhudi.model.request.PageRequest;
import com.zhudi.model.vo.UserVO;
import com.zhudi.utils.ResultUtils;
import com.zhudi.model.entity.User;
import com.zhudi.model.request.UserLoginRequest;
import com.zhudi.model.request.UserRegisterRequest;
import com.zhudi.expection.BusinessException;
import com.zhudi.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.zhudi.contant.UserContant.USER_LOGIN_STATE;

/**
 * 用户用户前端的控制器
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {


    @Resource
    UserService userService;

    /**
     * 登录接口
     * @param userLoginRequest 封装的登录数据传输类
     * @param request 浏览器request
     * @return 登录成功的话返回用户的相关信息
     */
    @PostMapping("/login")
    public BaseResponse<UserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){
        if (StringUtils.isAnyBlank(userLoginRequest.getUser_name(), userLoginRequest.getPassword())){
            throw new BusinessException(ErrorCode.USER_ERROR,"账户或密码为空");
        }
        User loginUser = (User) request.getSession().getAttribute(USER_LOGIN_STATE);
        if(loginUser!=null){
            String userName = loginUser.getUserName();
            //如果request中的用户名和要登陆的一致，说明已经登录了，那么就不用再次登录了
            if (userLoginRequest.getUser_name().equals(userName)){
                return ResultUtils.success(null,"不能重复登录");
            }
        }
        UserVO userVO = userService.login(userLoginRequest, request);
        if (userVO==null){
            return ResultUtils.Error(ErrorCode.PARAMS_ERROR,"用户名或密码错误");
        }
        return ResultUtils.success(userVO,"登录成功");
    }

    /**
     * 注册功能
     * @param request 浏览器request
     * @return 注册成功返回用户id，失败返回空
     */
    @PostMapping("/register")
    public BaseResponse<Integer> userRegister(@RequestBody UserRegisterRequest userRegisterRequest, HttpServletRequest request){

        if (StringUtils.isAnyBlank(userRegisterRequest.getUser_name(), userRegisterRequest.getPassword(), userRegisterRequest.getConfirmPassword())){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"注册信息参数不足");
        }
        Integer result = userService.register(userRegisterRequest, request);

        return ResultUtils.success(result ,"注册成功");
    }

    /**
     * 注销功能
     * @param request 浏览器request
     * @return 成功返回0
     */
    @PostMapping("/logout")
    public BaseResponse<Integer> userLogout(HttpServletRequest request){
        return ResultUtils.success(userService.logout( request ),"注销成功");
    }

    @GetMapping("/showAll")
    public BaseResponse<List<UserVO>> getAllUser(){
        //todo 后面这里可以加一个鉴权功能
        return ResultUtils.success(userService.getAllUser(),"获取脱敏后的所有用户信息成功");
    }

    @GetMapping("/currentUser")
    @UserLoginCheck
    public BaseResponse<UserVO> getCurrentUser(HttpServletRequest httpServletRequest){
        User user = (User)httpServletRequest.getSession().getAttribute(USER_LOGIN_STATE);
        User currentUser = userService.getCurrentUser(user.getId());
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(currentUser,userVO);
        return ResultUtils.success(userVO,"获取脱敏后的用户信息成功");

    }

    @PostMapping("/update")
    @UserLoginCheck
    public BaseResponse<Boolean> update(@RequestBody User user, HttpServletRequest request){
        //前端可以通过    [editUser.value.editKey as string]: editUser.value.currentValue,来直接指定要传输到哪一个字段
        //所以我们后端直接用一个User接受就可以，当然后期还要再改
        if (user==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = (User) request.getSession().getAttribute(USER_LOGIN_STATE);

        return ResultUtils.result(userService.update(user, loginUser));
    }


    @GetMapping("/getUserByPage")
    public BaseResponse<Page<UserVO>> getUserByPage(int pageSize, int pageNum, HttpServletRequest request){
        //暂时设置为没登陆也可以推荐用户到首页

        if(pageNum<1||pageSize<0){
            return ResultUtils.Error(ErrorCode.PARAMS_ERROR,"页数或者条数错误");
        }
        User loginUser = (User) request.getSession().getAttribute(USER_LOGIN_STATE);

        return ResultUtils.success(userService.getUserByPage(pageNum,pageSize,loginUser),ErrorCode.SUCCESS.getDescription());

    }

    @GetMapping("/match")
    @UserLoginCheck
    public BaseResponse<Page<UserVO>> match(PageRequest page , HttpServletRequest request){
        if (page==null){

            page =new PageRequest(1,10);

        }
        User loginUser = (User) request.getSession().getAttribute(USER_LOGIN_STATE);
        return ResultUtils.success(userService.matchUsersByTags(loginUser,page),"获取用户成功");
    }
}
